package com.beiding.codecreater.render.java.boframe.common;

import com.beiding.codecreater.render.java.utils.NameUtils;

import java.util.*;

public class BoData {

    private String name;
    private Set<String> quoteFieldNames = new HashSet<>();
    private Map<String,Field> fields = new HashMap<>();
    private Map<String,Quote> quotes = new HashMap<>();
    private Set<Holder> holders = new HashSet<>();

    private void checkAddName(String fn) {
        if (quoteFieldNames.contains(fn)) {
            throw new RuntimeException("重复字段:" + fn);
        }
        if (fn.equals("vo") || fn.equals("bo") || fn.equals("be") || fn.equals("id")) {
            throw new RuntimeException("保留字段:" + fn);
        }
        if (fn.startsWith("_")) {
            throw new RuntimeException("不能使用'_'作为前缀:" + fn);
        }
        NameUtils.checkCommonName(fn);
        quoteFieldNames.add(fn);
    }

    public void addField(Field field) {
        if (!allClass.contains(field.get_type())) {
            throw new RuntimeException("不支持的数据类型" + field.get_type());
        }
        checkAddName(field.getName());
        fields.put(field.getName(),field);
    }

    private static Map<String, Class> typeToClass = new HashMap<>();
    private static Set<Class> allClass = new HashSet<>();

    static {
        allClass.add(Date.class);
        allClass.add(String.class);
        allClass.add(Integer.class);
        allClass.add(Long.class);
        allClass.add(Double.class);
        allClass.add(Float.class);
        allClass.add(Boolean.class);
        allClass.add(Character.class);
        allClass.add(Byte.class);

        for (Class aClass : allClass) {
            typeToClass.put(aClass.getSimpleName(), aClass);
        }

    }

    public void setField(String type, String name) {
        Class aClass = typeToClass.get(type);
        if (aClass == null) {
            throw new RuntimeException("不支持的数据类型" + type);
        }
        addField(new Field(aClass, name));
    }

    public void setField(String type, String name, Integer length) {
        Class aClass = typeToClass.get(type);
        if (aClass == null) {
            throw new RuntimeException("不支持的数据类型" + type);
        }
        addField(new Field(aClass, name, length));
    }

    public void addQuote(Quote quote) {
        checkAddName(quote.getName());
        quoteFieldNames.add(quote.getName());
        quotes.put(quote.getName(),quote);
    }

    public void setQuote(BoData boData, String name) {
        if (boData == null) {
            throw new RuntimeException("元数据不能为空");
        }
        addQuote(new Quote(boData, name));
    }

    public Set<Holder> getHolders() {
        return holders;
    }

    public Collection<Quote> getQuotes() {
        return quotes.values();
    }
    public Collection<Field> getFields() {
        return fields.values();
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addHolder(BoData h, String selfName) {
        Holder holder = new Holder();
        holder.setType(h);
        holder.setSelfName(selfName);
        holders.add(holder);
    }


}
